package com.lee.common.utils

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import com.blankj.utilcode.util.ActivityUtils
import java.util.*

/**
 * Activity栈任务管理器
 * 通常放在我们自定义Activity的基类中操作
 */
class MyActivityManager private constructor() {

    private val activityStack: Stack<Activity> = Stack()

    companion object {
        val instance: MyActivityManager by lazy { MyActivityManager() }
    }

    /**
     * 添加Activity
     */
    fun addActivity(activity: Activity) {
        activityStack.add(activity)
    }

    fun activitySize(): Int {
        return activityStack.size
    }

    /**
     * 移出存在的Activity
     */
    fun removeActivity(activity: Activity) {
        if (activityStack.contains(activity)) {
            activity.finish()
            activityStack.remove(activity)
        }
    }

    /**
     * 获取最上面的Activity
     */
    fun getTopActivity(): Activity {
        return activityStack.lastElement()
    }

    /**
     * 清除Activity栈
     */
    private fun clearActivity() {
        for (activity in activityStack) {
            activity.finish()
        }
        activityStack.clear()
    }

    /**
     * 退出 只保留当前activity
     */
    fun holdBackActivity(aClass: Class<*>) {
        while (activityStack.size > 0) {
            val activity = getTopActivity()
            if (activity.javaClass == aClass) break
            popOneActivity(activity)
        }
    }

    fun popToActivity(aClass: Class<*>) {
        while (activityStack.size > 0) {
            val activity = getTopActivity() ?: break
            if (activity.javaClass == aClass) return
            popOneActivity(activity)
        }
    }

    fun popToActivity(aClass: Class<*>, isInclude: Boolean) {
        while (activityStack.size > 0) {
            val activity = getTopActivity() ?: break
            if (activity.javaClass == aClass) {
                if (isInclude) {
                    popOneActivity(activity)
                }
                return
            }
            popOneActivity(activity)
        }
    }

    fun isHas(aClass: Class<*>): Boolean {
        for (activity in activityStack) {
            if (activity == null) return false
            if (activity.javaClass == aClass) {
                return true
            }
        }
        return false
    }

    fun popOneActivity(activity: Activity?) {
        if (activity != null) {
            activityStack.remove(activity)
            activity.finish()
        }
    }


    /**
     * 退出所有activity
     */
    fun finishAllActivity() {
        while (activityStack.size > 0) {
            val activity = getTopActivity()
            popOneActivity(activity)
        }
    }


    /*
     * 退出应用程序
     */
    fun exitApp(context: Context) {
        //先清除Activity
        //finishAllActivity()
        ActivityUtils.finishAllActivities()
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        //再killProcesses
        activityManager.killBackgroundProcesses("com.hinnotek.amoeba")
        //System.exit(0)
    }
}

